package avicit.bdp.dgs.qa.utils.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-05-30
 * @类说明：RedisUtils
 * @修改记录：
 * @注意事项：
 * @主要功能：Redis操作公共类
 */
@Service
public class RedisUtils {
    private static final Logger logger = LoggerFactory.getLogger(RedisUtils.class);

    /**
     * 自增主键ID，支持多线程
     */
    private Map<String, RedisAtomicLong> map = new HashMap<>();

    @Resource
    private RedisTemplate redisTemplate;

    private static RedisUtils redisUtils = new RedisUtils();

    /**
     * 插入列表头部
     * @param key
     * @param value
     */
    public void lpush(final String key, final String value) {
        this.redisTemplate.execute(new RedisCallback<Void>() {
            public Void doInRedis(RedisConnection jedis) throws DataAccessException {
                jedis.lPush(encode(key), encode(value));
                return null;
            }
        });
    }

    /**
     * 移除列表的最后一个元素
     * @param key
     * @return
     */
    public String rpop(final String key) {
        return (String) this.redisTemplate.execute(new RedisCallback<String>() {
            public String doInRedis(RedisConnection jedis) throws DataAccessException {
                byte[] bytes = jedis.rPop(encode(key));
                return encode(bytes);
            }
        });
    }

    /**
     * 获取列表长度
     * @param key
     * @return
     */
    public long llen(final String key) {
        return ((Long) this.redisTemplate.execute(new RedisCallback<Long>() {
            public Long doInRedis(RedisConnection jedis) throws DataAccessException {
                return jedis.lLen(encode(key));
            }
        })).longValue();
    }

    /**
     * 移除列表元素
     * @param key
     * @param count
     * @param value
     * @return
     */
    public long lRem(final String key, final Long count, final String value) {
        return ((Long) this.redisTemplate.execute(new RedisCallback<Long>() {
            public Long doInRedis(RedisConnection jedis) throws DataAccessException {
                return jedis.lRem(encode(key), count, encode(value));
            }
        })).longValue();
    }

    /**
     * 加密
     * @param str
     * @return
     */
    public static byte[] encode(String str) {
        try {
            if (str == null || str.trim().length() == 0) {
                return new byte[0];
            }

            return str.getBytes("UTF-8");
        } catch (UnsupportedEncodingException unsupportedEncodingException) {
            return new byte[0];
        }
    }

    /**
     * 加密
     * @param data
     * @return
     */
    public static String encode(byte[] data) {
        try {
            if (data == null || data.length == 0) {
                return null;
            }

            return new String(data, "UTF-8");
        } catch (UnsupportedEncodingException unsupportedEncodingException) {
            return null;
        }
    }
}
